package com.ruoyi.jiajia.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.JiajiaOrder;
import com.ruoyi.system.domain.vo.IncreaseSumStatisticsVo;
import com.ruoyi.system.domain.vo.MapVo;
import com.ruoyi.system.service.IJiajiaOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/jiajia/statistics")
public class JiaJiaStatistics {

    private String prefix = "";

    @Resource
    private IJiajiaOrderService jiajiaOrderService;

    @RequiresPermissions("jiajia:statistics:view")
    @GetMapping()
    public String order()
    {
        return prefix + "/main_v1";
    }

//    @RequiresPermissions("jiajia:statistics:increaseSumStatistics")
    @GetMapping("/increaseSumStatistics")
    @ResponseBody
    public AjaxResult increaseSumStatistics() {
        JiajiaOrder  jiajiaOrder =new JiajiaOrder();
        Map<String ,Object > params=new HashMap<>();
        params.put("dataScope","");
        jiajiaOrder.setParams(params);
        IncreaseSumStatisticsVo increaseSumStatisticsVo=new IncreaseSumStatisticsVo();
        BigDecimal totalIncome =jiajiaOrderService.totalIncome(jiajiaOrder);
        BigDecimal yearIncome =jiajiaOrderService.yearIncome(jiajiaOrder);
        BigDecimal quarterIncome =jiajiaOrderService.quarterIncome(jiajiaOrder);
        BigDecimal monthIncome =jiajiaOrderService.monthIncome(jiajiaOrder);
        BigDecimal weekIncome =jiajiaOrderService.weekIncome(jiajiaOrder);
        Long totalOrderNum =jiajiaOrderService.totalOrderNum(jiajiaOrder);
        Long yearOrderNum =jiajiaOrderService.yearOrderNum(jiajiaOrder);
        Long quarterOrderNum =jiajiaOrderService.quarterOrderNum(jiajiaOrder);
        Long weekOrderNum =jiajiaOrderService.weekOrderNum(jiajiaOrder);
        Long monthOrderNum =jiajiaOrderService.monthOrderNum(jiajiaOrder);
        increaseSumStatisticsVo.setTotalIncome(totalIncome);
        increaseSumStatisticsVo.setYearOrderNum(yearOrderNum);
        increaseSumStatisticsVo.setQuarterIncome(quarterIncome);
        increaseSumStatisticsVo.setQuarterOrderNum(quarterOrderNum);
        increaseSumStatisticsVo.setWeekOrderNum(weekOrderNum);
        increaseSumStatisticsVo.setWeekIncome(weekIncome);
        increaseSumStatisticsVo.setWeekOrderNum(weekOrderNum);
        increaseSumStatisticsVo.setTotalOrderNum(totalOrderNum);
        increaseSumStatisticsVo.setYearIncome(yearIncome);
        increaseSumStatisticsVo.setMonthIncome(monthIncome);
        increaseSumStatisticsVo.setMonthOrderNum(monthOrderNum);
        LocalDate firstDay = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
        List<MapVo> barChart =new ArrayList<>();
        int daysInMonth = YearMonth.now().lengthOfMonth();
        for (int i = 1; i <= daysInMonth; i++) {
            MapVo barChartData=new MapVo();
            LocalDate date = firstDay.plusDays(i - 1);
            params.put("date",date);
            Long selectMonthOrderNum =jiajiaOrderService.selectMonthOrderNum(jiajiaOrder);
            BigDecimal monthIncomes =jiajiaOrderService.selectMonthIncome(jiajiaOrder);
            barChartData.setDate(date.toString());
            barChartData.setOrderNum(selectMonthOrderNum);
            barChartData.setIncome(monthIncomes);
            barChart.add(barChartData);
        }
        increaseSumStatisticsVo.setBarChart(barChart);

        return AjaxResult.success(increaseSumStatisticsVo);
    }
}
